// $Id$
// Licensed under Apache License Version 2.0
// See: http://www.apache.org/licenses/LICENSE-2.0.html
package de.servicezombie.tcmdb.dao.mongo;

import java.util.Collection;
import java.util.List;

import org.springframework.data.mongodb.core.query.Criteria;

import de.servicezombie.tcmdb.TcmdbRecordNotFoundException;

public interface SimpleCrudDao<DomainObject extends MongoDomainObject> {

	/**
	 * @return null if no record is found
	 */
	DomainObject get(String id);

	/**
	 * @throws TcmdbRecordNotFoundException
	 *             if no record is found
	 */
	DomainObject load(String id) throws TcmdbRecordNotFoundException;

	/**
	 * load a unique item by using criteria.
	 * 
	 * @throws TcmdbRecordNotFoundException
	 *             if there is not exactly one item returned
	 */
	DomainObject loadUnique(Criteria criteria) throws TcmdbRecordNotFoundException;

	/**
	 * overwrite an existing item which must exist on this call
	 * 
	 * @param object
	 *            item to overwrite in db
	 */
	DomainObject saveReplace(DomainObject object);

	/**
	 * overwrite an existing object or create a new one
	 * 
	 * @param object
	 *            item to save in db
	 */
	DomainObject saveNewOrReplace(DomainObject object);

	/**
	 * @param object
	 *            item to save in db
	 * @return currently saved item including its objectId
	 */
	DomainObject saveNew(DomainObject object);

	/**
	 * @param id
	 *            internal id of item to be deleted
	 */
	void delete(String id);

	/**
	 * @return all documents of a specified type / collection
	 */
	List<DomainObject> findAll();

	/**
	 * @return a subset of documents of a specified type / collection
	 */
	List<DomainObject> findByCriteria(Criteria criteria);

	/**
	 * @return a distinct list of all values of a specific field
	 */
	<V> Collection<V> findGroupedValues(String fieldName);
}
